﻿using CinemaMaestro.BusinessLayer.Contracts;
using CinemaMaestro.WebApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CinemaMaestro.WebApplication.Controllers
{
    public class SearchController : Controller
    {
        // GET: Search
        public ActionResult Index(string term)
        {
            var businessUoW = CinemaMaestro.Common.IocContainer.Resolver.Get<IBusinessObjectFactory>();
            var list = businessUoW.Movies.Read().ToList().Where(movie => movie.Title.ToLower().Contains(term.ToLower()));

            List<MovieViewModels> models = new List<MovieViewModels>();

            foreach (var model in list)
            {
                MovieViewModels movie = model;
                var base64 = model.Image != null ? Convert.ToBase64String(model.Image) : string.Empty;
                movie.Image = String.Format("data:image/jpg;base64,{0}", base64);
                models.Add(movie);
            }

            return Json(models, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Search(string movieName)
        {
            return View();
        }
    }
}